﻿using ApiLib.AppStart;
using SqlSugar;

namespace ApiLib.Cloud.Models;

/// <summary>
/// 云服务：网关配置表
///</summary>
[Tenant(GlobalConstants.MainDbConfigId), SugarTable("sys_cloud_zuul")]
public class ServiceInfo
{
    /// <summary>
    ///  
    ///</summary>
    [SugarColumn(ColumnName = "id", IsPrimaryKey = true, IsIdentity = true)]
    public int Id { get; set; }

    /// <summary>
    /// 关键字 
    ///</summary>
    [SugarColumn(ColumnName = "key")]
    public string Key { get; set; }

    /// <summary>
    /// 服务名 
    ///</summary>
    [SugarColumn(ColumnName = "name")]
    public string Name { get; set; }

    /// <summary>
    /// 拦截路径 
    ///</summary>
    [SugarColumn(ColumnName = "path")]
    public string Path { get; set; }

    /// <summary>
    /// 服务描述 
    ///</summary>
    [SugarColumn(ColumnName = "description")]
    public string Description { get; set; }

    /// <summary>
    /// 服务地址 
    ///</summary>
    [SugarColumn(ColumnName = "host")]
    public string Host { get; set; }

    /// <summary>
    /// 健康检查时间 
    ///</summary>
    [SugarColumn(ColumnName = "health_time")]
    public DateTime? HealthTime { get; set; }

    /// <summary>
    /// 注册时间 
    ///</summary>
    [SugarColumn(ColumnName = "register_time")]
    public DateTime? RegisterTime { get; set; }

    /// <summary>
    /// 是否在线：1在线0离线 
    ///</summary>
    [SugarColumn(ColumnName = "online")]
    public bool Online { get; set; } = true;

    /// <summary>
    /// 是否禁止：1禁止0允许 
    ///</summary>
    [SugarColumn(ColumnName = "forbidden")]
    public bool Forbidden { get; set; } = false;

    /// <summary>
    /// 请求量
    ///</summary>
    [SugarColumn(ColumnName = "visited_amount")]
    public int VisitedAmount { get; set; }

    /// <summary>
    /// 健康检查
    /// </summary>
    [SugarColumn(IsIgnore = true)]
    public string HealthUrl => Host + "/api/Cloud/Health";

    /// <summary>
    ///  服务类型：0心跳，1不心跳
    ///</summary>
    [SugarColumn(ColumnName = "type")]
    public int Type { get; set; } = 0;

    /// <summary>
    /// 是否正在执行
    /// </summary>
    [SugarColumn(IsIgnore = true)]
    public bool OnExecuting { get; set; }

    /// <summary>
    /// 重试次数
    /// </summary>
    [SugarColumn(ColumnName = "retry_count")]
    public int RetryCount { get; set; }

    /// <summary>
    /// 是否需要心跳
    /// </summary>
    /// <returns></returns>
    public bool NeedHeart()
    {
        return Type == 0;
    }
}